/** SimpleServer02.java: 
 *  The program doesn't work; it blocked by BufferedReader readLine().
 *  */

import java.io.*;
import java.net.*;

public class SimpleServer02 {

    public static void main(String arg[]){
	ServerSocket		ssk = null;
	BufferedReader		reader= null;
	Writer			writer = null;
	Socket			sk = null;
	String			msg = null;
	int			msgCount = 0;

	try {
	    ssk = new ServerSocket(9902);
	    sk = ssk.accept();
	    System.out.printf("A connection is made!\n");

	    reader = new BufferedReader(
		    	new InputStreamReader ( sk.getInputStream() ) );
	    writer = new BufferedWriter(
		    	new OutputStreamWriter(sk.getOutputStream() ) ) ;

	    int	k = 0;

	    while ( true ) {

		msg = reader.readLine(); 
		System.out.printf("SERVER: %s\n", msg );

		msg = "SERVER MESSAGE " + (++ msgCount);
	    	writer.write(msg, 0, msg.length()) ;
		writer.flush();

	    	Thread.sleep( 1000 );
	    }
	}
	catch(Exception e) { e.printStackTrace();  } 

    }

}
